package github.kawaiior.yuanchuxiuzhen.core.init.event;

import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.base.JinGuangXuanGong;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.merchant.villager.AbstractVillagerEntity;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class PlayerManaEvent {

    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event){
        if (event.phase != TickEvent.Phase.END || event.player.world.getGameTime() % 20 != 0){
            return;
        }
        PlayerEntity player = event.player;
        ManaPower.onPlayerTick(player);
    }

    @SubscribeEvent
    public static void onPlayerHurt(LivingHurtEvent event){
        LivingEntity entity = event.getEntityLiving();
        if (!(entity instanceof PlayerEntity) || entity.world.isRemote || entity instanceof FakePlayer){
            return;
        }
        PlayerEntity player = (PlayerEntity) entity;
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            // 根据manaPower的defense数值减免伤害
            float reduction = Math.max((1 - (power.getDefense() / 100F)), 0.2F);
            event.setAmount(event.getAmount() * reduction);
        });
    }

    @SubscribeEvent(priority = EventPriority.LOW)
    public static void onPlayerAttack(LivingHurtEvent event){
        Entity source = event.getSource().getTrueSource();
        if (source == null || !(source instanceof PlayerEntity) || source.world.isRemote || source instanceof FakePlayer){
            return;
        }
        PlayerEntity player = (PlayerEntity) source;
        // 如果攻击的是自己，那么不附加伤害
        if (event.getEntityLiving() instanceof PlayerEntity){
            PlayerEntity hurtPlayer = (PlayerEntity) event.getEntityLiving();
            if (hurtPlayer.equals(player)){
                return;
            }
        }
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            // 根据manaPower的attack数值提升伤害
            event.setAmount(event.getAmount() + power.getAttack() - 1);
        });

        JinGuangXuanGong.onPlayerAttack(player, event);
    }

    private static void onPlayerKillLiving(PlayerEntity player, LivingEntity living){
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            if (living instanceof IMob){
                // 加功德
                power.addMerit(player.world, player, 1);
            }else {
                // 减功德
                power.consumeMerit(player.world, player, 1);
            }
            if (living instanceof AbstractVillagerEntity){
                // 加煞气
                power.addEvilSpirit(player.world, player, 1);
            }
            // 加经验
            power.addExperience((int) Math.max(living.getMaxHealth()/4, 1), player.world, player);
        });
    }

    @SubscribeEvent
    public static void onEntityDeath(LivingDeathEvent event){
        LivingEntity living = event.getEntityLiving();
        Entity source = event.getSource().getTrueSource();

        if (!(source instanceof PlayerEntity) || source.world.isRemote){
            return;
        }

        PlayerEntity player = (PlayerEntity) source;
        onPlayerKillLiving(player, living);
    }

}
